<?php

use console\db\Migration;
use yii\rbac\DbManager;
use yii\base\InvalidConfigException;

/**
 * 创建RBAC相关信息表
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m150104_091301_t_rbac extends Migration {

    /**
     * @throws yii\base\InvalidConfigException
     * @return DbManager
     */
    protected function getAuthManager() {
        $authManager = Yii::$app->getAuthManager();
        if (!$authManager instanceof DbManager) {
            throw new InvalidConfigException('You should configure "authManager" component to use database before executing this migration.');
        }
        return $authManager;
    }

    /**
     * 权限规则
     * @todo create table `auth_rule`.
     */
    public function buildTableAuthRule() {
        $authManager = $this->getAuthManager();
        return [
            'name' => $authManager->ruleTable,
            'comment' => '权限规则',
            'columns' => [
                'name' => $this->string(64)->notNull()->comment('规则名称'),
                'data' => $this->binary()->comment('规则内容'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'PRIMARY KEY ([[name]])',
            ]
        ];
    }

    /**
     * 权限项
     * @todo create table `auth_item`.
     */
    public function buildTableAuthItem() {
        $authManager = $this->getAuthManager();
        return [
            'name' => $authManager->itemTable,
            'comment' => '权限项',
            'columns' => [
                'name' => $this->string(64)->notNull()->comment('名称'),
                'type' => $this->smallInteger()->notNull()->comment('类型'),
                'description' => $this->string(255)->comment('描述'),
                'rule_name' => $this->string(64)->comment('规则名称'),
                'data' => $this->binary()->comment('内容'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'PRIMARY KEY ([[name]])',
                'FOREIGN KEY ([[rule_name]]) REFERENCES ' . $authManager->ruleTable . ' ([[name]])' . $this->buildFkClause('ON DELETE SET NULL', 'ON UPDATE CASCADE'),
            ],
            'indexs' => [
                [
                    'colums' => 'type'
                ]
            ]
        ];
    }

    /**
     * 权限子项
     * @todo create table `auth_item_child`.
     */
    public function buildTableAuthItemChild() {
        $authManager = $this->getAuthManager();
        return [
            'name' => $authManager->itemChildTable,
            'comment' => '权限子项',
            'columns' => [
                'parent' => $this->string(64)->notNull()->comment('主项名称'),
                'child' => $this->string(64)->notNull()->comment('子项名称'),
                'PRIMARY KEY ([[parent]], [[child]])',
                'FOREIGN KEY ([[parent]]) REFERENCES ' . $authManager->itemTable . ' ([[name]])' . $this->buildFkClause('ON DELETE CASCADE', 'ON UPDATE CASCADE'),
                'FOREIGN KEY ([[child]]) REFERENCES ' . $authManager->itemTable . ' ([[name]])' . $this->buildFkClause('ON DELETE CASCADE', 'ON UPDATE CASCADE'),
            ]
        ];
    }

    /**
     * 权限授权表
     * @todo create table `auth_assignment`.
     */
    public function buildTableAuthAssignment() {
        $authManager = $this->getAuthManager();
        return [
            'name' => $authManager->assignmentTable,
            'comment' => '权限授权表',
            'columns' => [
                'item_name' => $this->string(64)->notNull()->comment('项名称'),
                'user_id' => $this->string(64)->notNull()->comment('管理员ID'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'PRIMARY KEY ([[item_name]], [[user_id]])',
                'FOREIGN KEY ([[item_name]]) REFERENCES ' . $authManager->itemTable . ' ([[name]])' . $this->buildFkClause('ON DELETE CASCADE', 'ON UPDATE CASCADE'),
            ],
            'indexs' => [
                [
                    'colums' => 'user_id'
                ]
            ]
        ];
    }

}
